# -*- coding: utf-8 -*-
"""
Created on Wed Mar 31 13:47:39 2021

@author: hp
"""

from selenium import webdriver;
from bs4 import BeautifulSoup;
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH;
from time import sleep;
from selenium.webdriver.common.keys import Keys;

#目的地址
DEST_URL = 'https://wenku.baidu.com/view/765179b2657d27284b73f242336c1eb91b373327.html';
doc_title = '';
doc_content_list = [];
def find_doc(driver,init = True):
    global doc_content_list
    global doc_title
    stop_condition = False;
    html = driver.page_source;
    soup1 = BeautifulSoup(html,'lxml');
    if (init is True):#得到标题
        title_result = soup1.find('div',attrs = {'class':'doc-title'});
        doc_title = title_result.get_text();#得到文档标题
        #拖动滚动条
        init_page = driver.find_element_by_xpath("//div[@class='fold-page-text']");
        print(type(init_page),init_page);
        driver.execute_script('arguments[0].scrollIntoView();',init_page);
        init_page.click();
        init = False;
    else:
        try:
            page = driver.find_element_by_xpath("//div[@id='reader-container']");
            #print(type(next_page),next_page);
            next_page = driver.find_element_by_class_name("pagerwg-button");
            station = driver.find_element_by_xpath("");
            driver.execute_script('arguments[0].scrollIntoView(false);',station);
            #js.executeScript("arguments[0].click();",next_page);
            #sleep(5)
            '''js = "window.scrollTo(508,600)"
            driver.execute_script(js)'''
            next_page.click();
        except:
            #结束条件
            print('找不到元素');
            stop_condition = True;
            #next_page.send_keys(Keys.ENTER)
            # 遍历所有的txt标签标定的文档，将其空格删除，然后进行保存
    content_result = soup1.find_all('p',attrs = {'class':'txt'});
    for each in content_result:
        each_text = each.get_text();
        if ' ' in each_text:
            text = each_text.repllace(' ','');
        else:
            text = each_text;
        #print(each_text);
        doc_content_list.append(text);
        #得到正文内容
    sleep(2);#防止加载过慢
    if stop_condition is False:
        doc_title, doc_content_list = find_doc(driver,init);
    return doc_title,doc_content_list
def save(doc_title,doc_content_list):
    document = Document();
    heading = document.add_heading(doc_title,0);
    heading.alignment = WD_ALIGN_PARAGRAPH.CENTER#居中显示
    for each in doc_content_list:
        document.add_paragraph(each);
    #字符处理编码问题
    t_title = doc_title.split()[0];
    #print(t_title);
    #document.save('2.docx');
    document.save('百度文库-%s.docx'% t_title);
    print("\n\nCompleted: %s.docx, to read."% t_title);
    #driver.quit();
if __name__ == '__main__':
    options = webdriver.ChromeOptions();
    options.add_argument('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36')
    driver = webdriver.Chrome(chrome_options=options);
    driver.get(DEST_URL);
    print("**********START***********");
    title ,content = find_doc(driver,True);
    save(title,content);
    driver.quit();